﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KOPLibrary.DataManagement;

namespace KOPLibrary.Forms
{
    public partial class SelectExcelDataForm : Form
    {

        private AbstractData internalSource = null;

        public SelectExcelDataForm()
        {
            InitializeComponent();
        }

        private ExcelManager manager;

        public static AbstractData Execute(ExcelManager manager, object cell)
        {
            SelectExcelDataForm form = new SelectExcelDataForm();
            form.manager = manager;
            return form.ExecuteSelect();
        }

        public AbstractData ExecuteSelect()
        {
            BindData(10);
            if (ShowDialog() == System.Windows.Forms.DialogResult.OK)
                return internalSource;
            else return null;
        }

        private void BindData(int maxRows)
        {            
            AbstractData ad =manager.RetrieveData(maxRows, ctlFirstRowAsHeader.Checked);
            //if(ad== null) 
            //{
            //    MessageBox.Show("Seçilen hücreler veri içermiyor","Uyarı");
            //    return;
            //}
            ctlAbstractDataViewer.DataSource = ad;
            if (maxRows != 0)
                ctlAbstractDataViewer.AuroResizeColumns();
        }

        private void SelectExcelDataForm_Load(object sender, EventArgs e)
        {

        }

        private void ctlPreview_Click(object sender, EventArgs e)
        {
            BindData(10);
        }

        private void ctlOK_Click(object sender, EventArgs e)
        {
            internalSource = manager.RetrieveData(0, ctlFirstRowAsHeader.Checked);
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void ctlFirstRowAsHeader_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void ctlPrevAllData_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        private void ctlFirstRowAsHeader_Click(object sender, EventArgs e)
        {
            BindData(10);
        }

        private void messageControl1_Load(object sender, EventArgs e)
        {

        }
    }
}
